package Messaging;

import java.io.DataInputStream;
import java.io.IOException;

import lejos.util.Delay;

public class BTReceive extends Thread {
	Boolean read = false;
	DataInputStream readStream;
	String command;
	public BTReceive(DataInputStream readStream)
	{
		this.readStream = readStream;
	}
	
	@Override
	public void run()
	{
		boolean run = true;
		while(run)
		{
			int length;
			try {
				length = readStream.readInt();
				byte[] readCommand = new byte[length];
				readStream.readFully(readCommand);
				command = new String(readCommand, "UTF-8");
				read = true;
			} catch (IOException e) {
				run = false;
			}
			
			Delay.msDelay(10);
		}
	}
	
	public synchronized boolean Available()
	{
		return read;
	}
	
	public synchronized String getCommand()
	{
		read = false;
		return command;
	}

}
